import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/pages/common/recommendedBooks.dart';
import 'package:ichinesereader/utils/helper.dart';

class LevelListItemLowLevel extends StatelessWidget {
  final box = Hive.box('appconfig');

  @override
  Widget build(BuildContext context) {
    var levelcolors = Constants.bookLevelColors
        .where((element) => element.level!.toInt() > 0)
        .toList();
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    String level = box.get('UserLevel').toString().replaceAll('level', '');

    return Container(
      padding: const EdgeInsets.all(
        5,
      ),
      child: ListView.separated(
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        physics: ScrollPhysics(),
        primary: false,
        itemCount: levelcolors.length,
        itemBuilder: (context, index) {
          return InkWell(
            onTap: () {
              if (level == levelcolors[index].level.toString()) {
                if (isStudent) {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/student/leveledreadings');
                  } else {
                    Get.offAllNamed('/student/leveledreadings');
                  }
                } else {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/individual/leveledreadings');
                  } else {
                    Get.offAllNamed('/individual/leveledreadings');
                  }
                }
              } else {
                BookSearchDto dto = new BookSearchDto();
                dto.proficiencyLevel = [];
                dto.proficiencyLevel
                    ?.add('level${levelcolors[index].level.toString()}');

                // _booksController.searchedBookItems.clear();
                // _booksController.fetchSearchedBookItems(0, 0, dto, 'level');
                Get.to(() => RecommendedBooksPage(
                    title: 'Level ${levelcolors[index].level.toString()}',
                    bookSearchDto: dto));
              }
            },
            child: Stack(
              children: [
                Center(
                  child: Container(
                    height: 100,
                    width: 100,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color:
                          Get.isDarkMode ? Color(0xff111111) : Colors.white10,
                      boxShadow: [
                        BoxShadow(
                          blurRadius: 10,
                          spreadRadius: 1,
                          color: Get.isDarkMode
                              ? Color(0xff666666)
                              : hexStringToHexInt(
                                  levelcolors[index].color.toString()),
                          offset: Offset(3, 3),
                        ),
                      ],
                    ),
                  ),
                ),
                Center(
                  child: Container(
                    height: 100,
                    width: 100,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Get.isDarkMode
                          ? Color(0xff111111)
                          : Colors.white.withOpacity(0.1),
                      boxShadow: [
                        BoxShadow(
                          blurRadius: 2,
                          spreadRadius: 1,
                          color: Get.isDarkMode
                              ? Color(0xff666666).withOpacity(0)
                              : Colors.white.withOpacity(0.1),
                          offset: Offset(-4, -4),
                        ),
                        BoxShadow(
                          blurRadius: 10,
                          spreadRadius: 1,
                          color: Get.isDarkMode
                              ? Color(0xff666666)
                              : Colors.white.withOpacity(0.1),
                          offset:
                              Get.isDarkMode ? Offset(1, 1) : Offset(11, 11),
                        ),
                      ],
                    ),
                  ),
                ),
                Center(
                    child: Container(
                  margin: EdgeInsets.only(top: 56, left: 33),
                  child: Column(
                    children: [
                      Container(
                        child: Text(
                          'Level'.tr,
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 14,
                              fontFamily: 'Montserrat',
                              color: Get.isDarkMode
                                  ? Color(0xffcccccc)
                                  : Colors.white),
                        ),
                      ),
                      Container(
                        child: Text(
                          levelcolors[index].level.toString(),
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 37,
                              fontFamily: 'Montserrat',
                              color: Get.isDarkMode
                                  ? Color(0xffcccccc)
                                  : Colors.white),
                        ),
                      ),
                    ],
                  ),
                )),
              ],
            ),
          );
        },
        separatorBuilder: (context, _) => SizedBox(width: 10),
      ),
    );
  }
}
